package com.chinaztt.fda.http;

import android.app.Activity;
import android.text.TextUtils;

import com.chinaztt.fda.utils.NetworkManager;
import com.chinaztt.fda.utils.ProgressUtil;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class Communication {
	private static String TAG = new Throwable().getStackTrace()[0].getClassName();
	private static Communication mInstance = null;
	private Activity mActivity;

	private Communication() {
	}

	public static Communication getInstance() {
		if (mInstance == null) {
			mInstance = new Communication();
		}
		return mInstance;
	}

	public void init(Activity activity) {
		mActivity = activity;
	}


	public void post(final String message,final String url, ArrayList<NameValuePair> params,
					 final HttpRequestListener listener) {
		checkNetwork();
		if (!TextUtils.isEmpty(message)) {
			ProgressUtil.show(mActivity, message);
		}else {
			ProgressUtil.show(mActivity, "数据发送中...");
		}

		ProgressUtil.setOnKeyListener(new ProgressUtil.OnKeyListener() {
			@Override
			public void onCancel() {
				ProgressUtil.cancel();
				if (listener != null) {
					listener.setCancel(true);
					listener.onError(HttpState.ERR_CODE_USER_CANCEL,"用户取消");
				}
			}
		});

		HttpRequest.getInstance().post(url,
				params,new HttpRequestListener() {

					@Override
					public void onSuccess(JSONObject json) {
						ProgressUtil.cancel();
						try {
							String resultCode = json.getJSONObject("msg").getString("code");
							if(TextUtils.isEmpty(resultCode)){
								notifyError(listener,HttpState.ERR_CODE_HTTP_RESPOND,HttpState.ERROR_RESPON_COMMON);
								return;
							}
							if(!GV.RESULT_CODE_SUCCESS.equals(resultCode)) {
								String resultText = json.getJSONObject("msg").getString("text");
								if(TextUtils.isEmpty(resultText)) {
									resultText = HttpState.ERROR_RESPON_COMMON;
								}
								notifyError(listener,HttpState.ERR_CODE_HTTP_RESPOND,resultText);
								return;
							}
							if (listener != null && !listener.isCancelled()) {
								JSONObject dataObj = json.getJSONObject("data");
								if(dataObj!=null) {
									listener.onSuccess(dataObj);
								}
								else {
									notifyError(listener,HttpState.ERR_CODE_HTTP_RESPOND,HttpState.ERROR_RESPON_NO_DATA);
								}
							}
						} catch (JSONException e) {
							e.printStackTrace();
							notifyError(listener,HttpState.ERR_CODE_HTTP_RESPOND,HttpState.ERROR_RESPON_COMMON);
							return;
						}
					}

					@Override
					public void onStateChange(String stateChangeMsg) {
						ProgressUtil.show(mActivity, stateChangeMsg);
						if (listener != null && !listener.isCancelled()) {
							listener.onStateChange(stateChangeMsg);
						}
					}

					@Override
					public void onError(int errorCode, String errorMsg) {
						ProgressUtil.cancel();
						notifyError(listener,errorCode,errorMsg);
					}
				});
	}

	private void checkNetwork() {
		if ("gprs".equals(GlobalVar.getInstance().getStringValue(GV.NET_TYPE,"gprs"))) {
			boolean isOpen = NetworkManager.getMobileDataStatus(mActivity);
			if (!isOpen) {
				NetworkManager.setMobileDataStatus(mActivity, true);
			}
		}
	}

	public static void notifyError(HttpRequestListener listener,int errorCode, String errorMsg) {
		if (listener != null && !listener.isCancelled()) {
			listener.onError(errorCode,errorMsg);
		}
	}
}
